package com.by_man.presenter.module_health_management.health_advice;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.method.ScrollingMovementMethod;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;
import com.by_man.presenter.R;
import com.by_man.presenter.SharedApplication;
import com.by_man.presenter.module_health_consultant.BitmapCache;
import com.by_man.repository.IpAddress;
import com.by_man.repository.LocalDataManager;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * 用户选择查看的建议
 * Created by man on 2017-02-18.
 */
public class ChosedAdviceActivity extends AppCompatActivity {
    private ImageLoader imageLoader;
    private RequestQueue rQueue;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chosed_advice);
        //rQueue = Volley.newRequestQueue(this);
        //imageLoader = new ImageLoader(rQueue,new BitmapCache());
        imageLoader = SharedApplication.getImageLoader(getApplication());
        initView();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
//                Intent intent = new Intent(this,HealthAdviceActivity.class);
//                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//                startActivity(intent);
                onBackPressed();
                break;
            default:
                break;
        }
        return super.onOptionsItemSelected(item);
    }

//    @Override
//    public boolean onKeyDown(int keyCode, KeyEvent event) {
//        if(keyCode == KeyEvent.KEYCODE_BACK) {
//            Intent intent = new Intent(this,HealthAdviceActivity.class);
//            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//            startActivity(intent);
//        }
//
//        return super.onKeyDown(keyCode, event);
//    }

//    protected void onRestart()
//    {
//        super.onRestart();
//        Intent intent=new Intent(this,GestureLockActivity.class);
//        intent.putExtra("class",this.getClass().getName());
//        intent.putExtra("position", 0);
//        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//        startActivity(intent);
//    }

    private void initView() {
        // 初始化ToolBar
        Toolbar toolbar = (Toolbar)findViewById(R.id.tool_bar);
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        actionBar.setHomeAsUpIndicator(R.drawable.icon_back);
        actionBar.setDisplayHomeAsUpEnabled(true);

        // 为TextView 设定文字内容
        final Intent intent = getIntent();

            final int postion = intent.getIntExtra("positionID", 0);
            JSONArray jsonArray = LocalDataManager.getAllHealthAdviceRecord();
            if (jsonArray != null) {
                try {
                final JSONObject jsonObject = (JSONObject) jsonArray.get(postion);
                TextView type = (TextView) findViewById(R.id.type);
                TextView content = (TextView) findViewById(R.id.content);
                // 这句作用？？
                content.setMovementMethod(ScrollingMovementMethod.getInstance());
                TextView updatetime = (TextView) findViewById(R.id.updatetime);
                final ImageView picAdvice = (ImageView) findViewById(R.id.pic_advice);

                // 取出数据进行设定
                type.setText(jsonObject.getString("leixing"));
                content.setText(jsonObject.getString("advice"));
                updatetime.setText(jsonObject.getString("update_at"));

                // 取出图片路径
                String pic_url = jsonObject.getString("pic");
                // 判断合法性
                if (pic_url.length() != 4) {
                    pic_url = IpAddress.Load_Image + pic_url;
                    final String pic_path = pic_url;
                    ImageLoader.ImageListener listener = ImageLoader.getImageListener(picAdvice,
                            R.drawable.no,R.drawable.no);
                    imageLoader.get(pic_url,listener);
                    picAdvice.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            //
                            Intent intent_pic = new Intent(ChosedAdviceActivity.this, PhotoViewActivity.class);
                            intent_pic.putExtra("position", postion);
                            intent_pic.putExtra("pic_path", pic_path);
                            ChosedAdviceActivity.this.startActivity(intent_pic);
                        }
                    });
                } else {
                    LinearLayout text = (LinearLayout) findViewById(R.id.text);
                    text.setVisibility(View.GONE);
                    TextView picWord = (TextView) findViewById(R.id.picword);
                    picWord.setVisibility(View.GONE);
                }
            }catch(JSONException je){
                je.printStackTrace();
            }
        }
    }
}
